/**
 * 
 */
package insigne.gui.mainFrame.clipboard;

import insigne.gui.mainFrame.tabbedPane.CommandeTabbedPane;
import insigne.kernel.sgbdinterface.basics.Insigne;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.FlavorEvent;
import java.awt.datatransfer.FlavorListener;
import java.awt.datatransfer.StringSelection;
import java.util.StringTokenizer;



/**
 * Classe surveillant le presse papier. Permet d'importer les commandes du forum et des insignes du site
 * @author Xavier
 */
public class ClipBoardListener implements FlavorListener {
	/* ############################### Fields ############################### */
	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	/* ########################## Getters And Setters ####################### */

	/* ########################### Class Constructors ####################### */
	/** Class constructor */
	public ClipBoardListener() {
		this.clipboard.addFlavorListener(this);
	}

	/* ############################### Methods ############################## */
	/** 
	 * @see java.awt.datatransfer.FlavorListener#flavorsChanged(java.awt.datatransfer.FlavorEvent)
	 */
	@Override
	public void flavorsChanged(FlavorEvent arg0) {
		try {
			String content = (String)clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);
			System.out.println(content);
			StringTokenizer tok = new StringTokenizer(content);
			String token = tok.nextToken();
			if (token.equalsIgnoreCase("Spoiler")){
				CommandeTabbedPane.commandeCopier.setText(content);
				clipboard.setContents(new StringSelection(""), null);
			}
			String categorie = "blablablabla";
			while (tok.hasMoreTokens()){
				if (token.equals("Votre")){
					if(tok.nextToken().equals("Panier")){
						categorie = tok.nextToken();
					}
				}
				if (token.contains(categorie) || token.matches("[A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][A-Z][0-9][0-9][0-9]|[A-Z][A-Z][A-Z][A-Z][A-Z][0-9]")){
					String reference = token;
					float prix = Float.parseFloat(tok.nextToken().replace(",", "."));
					tok.nextToken();tok.nextToken();tok.nextToken();tok.nextToken();
					String nom = tok.nextToken();
					token = tok.nextToken();
					while (!token.contains(categorie) && !token.matches("[A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][A-Z][0-9][0-9][0-9]|[A-Z][A-Z][A-Z][A-Z][A-Z][0-9]") && !token.equalsIgnoreCase("CONDITIONS")){
						nom += " " + token;
						token = tok.nextToken();
					}
					new Insigne(nom,prix, 0,reference);
				}
				else{
					token = tok.nextToken();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void main(String[] args) {
		System.out.println("AAA12".matches("[A-Z][A-Z][0-9][0-9]|[A-Z][A-Z][A-Z][0-9][0-9]"));
	}
}
